package com.jsgt.greekfire.ui.trading.fragment

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fhx.core.base.BaseFragment
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.Tick
import com.jsgt.greekfire.ui.trading.adapter.VolumeAdapter
import kotlinx.android.synthetic.main.fragment_depth.*
import kotlinx.android.synthetic.main.fragment_volume.*

private const val ARG_PARAM1 = "param1"

class VolumeFragment : BaseFragment() {
    private var param1: String? = null

    var tickList: List<Tick>? = null
    private val volumeAdapter: VolumeAdapter by lazy { VolumeAdapter() }
    private var isScroll = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_volume
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String) =
            VolumeFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                }
            }
    }

    override fun main() {
        val symbolArray = param1?.split("_")
        if (!symbolArray.isNullOrEmpty() && symbolArray.size > 1) {
            tvNumberV.text = "${resources.getText(R.string.number)}(${symbolArray?.get(0)})"
            tvPriceV.text = "${resources.getText(R.string.price)}(${symbolArray?.get(1)})"
        }else{
            tvNumberV.text = "${resources.getText(R.string.number)}(--)"
            tvPriceV.text = "${resources.getText(R.string.price)}(--)"
        }

//        rvVolume.isNestedScrollingEnabled = false
        rvVolume.layoutManager = LinearLayoutManager(context)
        rvVolume.adapter = volumeAdapter

        //记录滑动状态，如果滑动中不需要刷新页面，静止时立即刷新
        rvVolume.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    isScroll = false
                    if (tickList != null) {
                        volumeAdapter.refreshData(tickList!!)
                    }
                } else {
                    isScroll = true
                }
            }
        })
        if (tickList != null) {
            volumeAdapter.refreshData(tickList!!)
        }
    }

    fun setData(tickList: List<Tick>) {
        this.tickList = tickList
        if (!isScroll) {
            volumeAdapter.refreshData(tickList)
        }
    }

    fun setDataNoRefresh(tickList: List<Tick>) {
        this.tickList = tickList
    }

    fun refresh() {
        if (tickList != null) {
            volumeAdapter.refreshData(tickList!!)
        }
    }
}